﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using Sukul.PhotoTools.BL;

namespace Sukul.PhotoTools.UI
{
    public partial class MediaImporter : Form
    {
        FileMaster fm = new FileMaster();
        public MediaImporter()
        {
            InitializeComponent();
            fm.FileCopy_Progress += new FileMaster.del_Progress(fm_FileCopy_Progress);
            pb.Minimum = 0;
            pb.Step = 1;
        }

        void fm_FileCopy_Progress(object parent, string message, int processedItemCount, int totalItemCount, int skippedFileCount)
        {
            decimal progress;
            progress = ((decimal)processedItemCount / (decimal)totalItemCount);
            progress *= 100;
            if (processedItemCount >= totalItemCount) { progress = 100; }
            //* 100;
            backgroundWorker.ReportProgress((int)Math.Floor(progress));
            lblStatus.Invoke((Action)delegate { lblStatus.Text = message; });
        }

        private void btnFolderSelect_Click(object sender, EventArgs e)
        {
            if (folderBrowse.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                lblStartFolder.Text = folderBrowse.SelectedPath;
            }
        }

        private void btnFolderEndSelect_Click(object sender, EventArgs e)
        {
            if (folderBrowse.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                lblPhotoEndFolder.Text = folderBrowse.SelectedPath;
            }
        }

        private void btnVideoFolderEndSelect_Click(object sender, EventArgs e)
        {
            if (folderBrowse.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                lblVideoEndFolder.Text = folderBrowse.SelectedPath;
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            if (btnImport.Text == "Cancel Import")
            {
                btnImport.Text = "Cancelling..";
                btnImport.Enabled = false;
                backgroundWorker.CancelAsync();
                FileMaster.Cancel = true;
            }
            else
            {
                if (!backgroundWorker.IsBusy)
                {
                    lblStatus.Text = "";
                    btnImport.Text = "Cancel Import";
                    backgroundWorker.RunWorkerAsync();
                }
            }
        }

        private void MediaImporter_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var section = (Sukul.PhotoTools.BL.Configuration.MediaConfigSection)config.GetSection("MediaSettingsGroup/MediaSettings");
            section.SourceFolder = lblStartFolder.Text;
            section.PhotoDestinationFolder = lblPhotoEndFolder.Text;
            section.VideoDestinationFolder = lblVideoEndFolder.Text;

            config.Save(ConfigurationSaveMode.Modified);
        }

        private void MediaImporter_Load(object sender, EventArgs e)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var section = (Sukul.PhotoTools.BL.Configuration.MediaConfigSection)config.GetSection("MediaSettingsGroup/MediaSettings");
            lblStartFolder.Text = section.SourceFolder;
            lblPhotoEndFolder.Text = section.PhotoDestinationFolder;
            lblVideoEndFolder.Text = section.VideoDestinationFolder;
        }

        private void btnExploreStart_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(lblStartFolder.Text);
        }

        private void btnExploreEnd_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(lblPhotoEndFolder.Text);
        }

        private void btnVideoExploreEnd_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(lblVideoEndFolder.Text);
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            DisableControls();
            fm.SetFolders(lblStartFolder.Text, lblPhotoEndFolder.Text, lblVideoEndFolder.Text);
            fm.ProcessFiles();
        }

        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (pb.InvokeRequired)
            {
                pb.Invoke((Action)delegate { pb.Value = e.ProgressPercentage; });
            }
            else
            {
                pb.Value = e.ProgressPercentage;
            }
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            btnImport.Text = "Import";
            btnImport.Enabled = true;
            EnableControls();
            if (e.Cancelled)
            {
                lblStatus.Text = "Cancelled";
            }
            else if (null != e.Error)
            {
                lblStatus.Text = "Error: " + e.Error.Message;
            }
            else
            {
                lblStatus.Text = "Done!";
            }

        }

        private void DisableControls()
        {
            btnFolderStartSelect.Invoke((Action)delegate { btnFolderStartSelect.Enabled = false; });
            btnVideoFolderEndSelect.Invoke((Action)delegate { btnVideoFolderEndSelect.Enabled = false; });
            btnPhotoFolderEndSelect.Invoke((Action)delegate { btnPhotoFolderEndSelect.Enabled = false; });
        }

        private void EnableControls()
        {
            btnFolderStartSelect.Invoke((Action)delegate { btnFolderStartSelect.Enabled = true; });
            btnPhotoFolderEndSelect.Invoke((Action)delegate { btnPhotoFolderEndSelect.Enabled = true; });
            btnVideoFolderEndSelect.Invoke((Action)delegate { btnVideoFolderEndSelect.Enabled = true; });
        }      
    }
}
